export default {
  // 开启命名空间
  namespaced: true,

  // state 数据
  state: () => ({
    // 收货地址
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '',
    userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
    jumpInfo: {}
  }),

  // 方法
  mutations: {
    // 更新收货地址
    updateAddress(state, address) {
      state.address = address
      this.commit('m_user/saveAddressStorage')
    },
    saveAddressStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    
    // 更新用户信息
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
      this.commit('m_user/saveUserInfoStorage')
    },
    saveUserInfoStorage(state) {
      uni.setStorageSync('userInfo', JSON.stringify(state.userInfo))
    },
    
    // 更新 token
    updateToken(state, token) {
      state.token = token
      this.commit('m_user/saveTokenStorage')
    },
    saveTokenStorage(state) {
      uni.setStorageSync('token', state.token)
    },
    
    // 更新跳回页面的信息
    updateJumpInfo(state, info) {
      state.jumpInfo = info
    },
    
  },

  // 数据包装器
  getters: {
    addstr(state) {
      const { provinceName, cityName, countyName, detailInfo } = state.address
      if (provinceName) {
        return provinceName + cityName + countyName + detailInfo
      } else {
        return ''
      }
    }
    
  },
}